home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8072 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.0 KB  |  40 lines

  1. Path: hp.fciencias.unam.mx!usenet
  2. From: Ernesto <gsr@servidor.unam.mx>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How do I overload 'char*' in my class. Is it possible?
  5. Date: 14 Feb 1996 07:35:43 GMT
  6. Organization: A poorly-installed InterNetNews site
  7. Message-ID: <4fs3cf$4a4@hp.fciencias.unam.mx>
  8. NNTP-Posting-Host: slip22.noc.unam.mx
  9.  
  10. wawda@alcor.usc.edu (Abu Wawda) writes:
  11. > I wrote a string class and now I would like to overload: char*
  12. > (character pointer), but can't figure out how to do it. I've already
  13. > overloaded the +, +=, ==, =, and some operator operators, but can't
  14. > figure how to do this (or even if it's possible). Basically I want
  15. > something like:
  16. >     char* operator ?? ()
  17. > so that my string class can also return char*. Please help. Thanks,
  18. > Abu Wawda
  19. > wawda@scf.usc.edu
  20.  
  21. Try this:
  22.  
  23. class String {
  24. public:
  25. //...
  26.    operator const char* () const {return rep;}
  27. //...
  28. private:
  29.  char *rep;
  30. };
  31.  
  32. Maybe you should read the third chapter of the book "Advanced C++.
  33. Programming Styles and Idioms", by James O. Coplien, Addisson Wesley.
  34.  
  35. Ernesto Silva
  36.